#include "lib.h"

#include "GameDBInterface.h"

#include "opts.h"
#include "SicBoSQL.h"
#include "crapssql.h"

#include "crapconn.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     GameDBInterface::GameDBInterface(CrapsConnection *pConn, TrackSQL *pSQL) 
///
/// Constructor.
///
/// @param  pConn   -   (in)    pointer back to object that represents the connection to the player.
/// @param  pSQL    -   (in)    pointer to singleton TrackSQL object, which really controls access to the database.
///
///////////////////////////////////////////////////////////////////////////////////////////////////////////
GameDBInterface::GameDBInterface(CrapsConnection *pConn, TrackSQL *pSQL) 
{ 
    m_pConn     = pConn;
    m_pTrackSQL = pSQL; 
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     GameDBInterface::~GameDBInterface() 
///
/// Destructor.
///
///////////////////////////////////////////////////////////////////////////////////////////////////////////
GameDBInterface::~GameDBInterface() 
{ 

}



///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     GameDBInterface::CreateInterface(CrapsConnection *pConn, TrackSQL *pSQL)
///
/// This STATIC method returns an instance of the correct derived class for the current game.
///
/// @param  pConn   -   (in)    pointer back to object that represents the connection to the player.
/// @param  pSQL    -   (in)    pointer to singleton TrackSQL object, which really controls access to the database.
///
///////////////////////////////////////////////////////////////////////////////////////////////////////////
GameDBInterface * GameDBInterface::CreateInterface(CrapsConnection *pConn, TrackSQL *pSQL)
{
    GameDBInterface * pRetVal = NULL;

    switch(pConn->getCurrentGameId())
    {
        case CRAPS_GAME_ID:
        {
            pRetVal = new CrapsSQL(pConn, pSQL);
            break;
        }
        case SICBO_GAME_ID:
        {
            pRetVal = new SicBoSQL(pConn, pSQL);
            break;
        }
    }

    return pRetVal;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn   GameDBInterface::trackGetBalance(int32 *balance, int32 *compBalance, int32 *bonusBalance)
///
/// Returns the balance information for the current player.
///
/// @param  balance     -   (out)   the display balance for the player
/// @param  compBalance -   (out)   not sure
/// @param  bonusBalance-   (out)   the portion of the total balance that comes from the coupons and whatnot
///
/// @return bool8   true if successful, false on failure
///////////////////////////////////////////////////////////////////////////////////////////////////////////
bool8 GameDBInterface::trackGetBalance(int32 *balance, int32 *compBalance, int32 *bonusBalance)
{
    CrapsConnection::Info *info = m_pConn->getCasinoSpecificInfo();
	int32 result = TrackSQL::ExecFailed;
	*balance=0;
	*compBalance=0;
	*bonusBalance=0;

	if (m_pTrackSQL && info)
	{
		m_pTrackSQL->lock();
		result = m_pTrackSQL->getBalance(ServerOpts::casinoId, info->pid, info->forMoney, balance, compBalance, bonusBalance);
		m_pTrackSQL->unlock();
	}

	return (bool8)(result == TrackSQL::NoError);
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @fn     GameDBInterface::trackAdjustBalance(int32 Amount)
///
/// Forces a change in the player's balance.  Not normally used.
///
/// @param  Amount  - amount to adjust the balance by
///
/// @return int32   returns the new balance
///////////////////////////////////////////////////////////////////////////////////////////////////////////
int32 GameDBInterface::trackAdjustBalance(int32 Amount)
{
    int32 nBal = 0;
    CrapsConnection::Info *info = m_pConn->getCasinoSpecificInfo();
	int32 result = TrackSQL::ExecFailed;
	int32 comp=0;

	if (m_pTrackSQL && info)
	{
		m_pTrackSQL->lock();
		result = m_pTrackSQL->adjustBalance(ServerOpts::casinoId, info->pid, info->forMoney, Amount, &nBal, &comp);
		m_pTrackSQL->unlock();
	}

	if (result != TrackSQL::NoError)
    {
		nBal = 0;
    }

	return nBal;
}
